import sys
from PySide2.QtWidgets import QApplication, QMainWindow, QPushButton
from PySide2.QtUiTools import QUiLoader
from PySide2.QtCore import QFile
from PySide2.QtGui import QPixmap, QIcon

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setup_ui("./main_window.ui")
        self.setWindowTitle("路径优化软件")
        self.setFixedSize(930, 670)#设置固定窗口,避免走形
        
        # 添加跳转按钮
        self.ui.btn_1.clicked.connect(self.open_second_window)
    
    def setup_ui(self, ui_file):
        """加载UI文件的通用方法"""
        loader = QUiLoader()
        file = QFile(ui_file)
        file.open(QFile.ReadOnly)
        self.ui = loader.load(file, self)
        file.close()
        
        self.setCentralWidget(self.ui)
        self.ui.label.setPixmap(QPixmap("./图片.png"))#在Qlabel中加载图片
        
    
    def open_second_window(self):
        """打开第二个窗口"""
        self.second_window = SecondWindow()
        self.second_window.show()
        # 可选：隐藏主窗口
        self.hide()

class SecondWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setup_ui("./two_window.ui")
        self.setWindowTitle("模拟退火算法")
        self.setFixedSize(930, 670)#设置固定窗口,避免走形
        self.ui.btn_back.clicked.connect(self.back_to_main)
    
    def setup_ui(self, ui_file):
        """加载UI文件的通用方法"""
        loader = QUiLoader()
        file = QFile(ui_file)
        file.open(QFile.ReadOnly)
        self.ui = loader.load(file, self)
        file.close()
        
        # self.setCentralWidget(self.ui)
        self.ui.label.setPixmap(QPixmap("./图片.png"))#在Qlabel中加载图片
    
    def back_to_main(self):
        """返回主界面"""
        self.close()
        # 如果之前隐藏了主窗口，需要重新显示
        main_window.show()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    app.setWindowIcon(QIcon('./logo.jpg'))#加载小图标
    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec_())